package com.sskj.hhrt.transaction;

import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.sskj.base.adapter.MyFragmentPagerAdapter;
import com.sskj.base.data.User;
import com.sskj.base.entry.TabItem;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.CommonTabLayout;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.hhrt.Constant;
import com.sskj.hhrt.LiveDataBus;
import com.sskj.hhrt.MVPFragment;
import com.sskj.hhrt.MyApplication;
import com.sskj.hhrt.R;
import com.sskj.hhrt.api.SystemInfo;
import com.sskj.hhrt.market.bean.MarketData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/03/20
 */
public class TransactionHomeFragment extends MVPFragment<TransactionHomePresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.tv_freeze_promise_money)
    TextView tvFreezePromiseMoney;
    @BindView(R.id.tv_dynamic_rights)
    TextView tvDynamicRights;
    @BindView(R.id.tv_hazard_rate)
    TextView tvHazardRate;
    @BindView(R.id.tv_hazard_rate_msg)
    TextView tvHazardRateMsg;
    @BindView(R.id.transaction_tabLayout)
    CommonTabLayout transactionTabLayout;
    @BindView(R.id.transaction_viewPager)
    FrameLayout transactionViewPager;
    private Disposable disposable;
    ArrayList<Fragment> fragments = new ArrayList<>();
    private boolean started;
    private String baoPer = "10%";
    private MarketData marketData;
    private ArrayList<CustomTabEntity> tabs = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_transaction_home;
    }

    @Override
    public TransactionHomePresenter getPresenter() {
        return new TransactionHomePresenter();
    }

    @Override
    public void initView() {
        toolbar.setTitle("交易");
        List<String> titles = Arrays.asList(getResources().getStringArray(R.array.app_transaction_titles));
        tabs.add(new TabItem(titles.get(0)));
        tabs.add(new TabItem(titles.get(1)));
        tabs.add(new TabItem(titles.get(2)));
        tabs.add(new TabItem(titles.get(3)));
        fragments.add(BuyInFragment.newInstance(Constant.BUY)); // 融资
        fragments.add(BuyInFragment.newInstance(Constant.SELL));  // 融券
//        fragments.add(GuaDanFragment.newInstance());// 挂单
        fragments.add(HoldPositionFragment.newInstance());  // 持仓
        fragments.add(DealFragment.newInstance());
        transactionTabLayout.setTabData(tabs, getChildFragmentManager(), R.id.transaction_viewPager, fragments);
//        fragments.add(CheckFragment.newInstance());// 查询
//        transactionViewPager.setOffscreenPageLimit(3);
//        transactionViewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), titles, fragments));
//        transactionTabLayout.setViewPager(transactionViewPager);
//        transactionTabLayout.setupWithViewPager(transactionViewPager);
//        transactionViewPager.setCurrentItem(0);

    }

    @Override
    public void initData() {
        ClickUtil.click(tvHazardRateMsg, view -> {
                    new AlertDialog.Builder(getContext())
                            .setTitle("爆仓率说明")
                            .setMessage("当爆仓率<=" + baoPer + "%时,系统会自动强平")
                            .setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                }
        );
    }


    public void setIndex(int index, MarketData coin) {
        transactionTabLayout.setCurrentTab(index);
        BuyInFragment fragment = (BuyInFragment) fragments.get(0);
        BuyInFragment fragment1 = (BuyInFragment) fragments.get(1);
        fragment.setCoin(coin);
        fragment1.setCoin(coin);
    }

    @Override
    public void lazyLoad() {
        started = true;
        mPresenter.getAccountDetail(getAccount(), true);
        mPresenter.getSystemInfo();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            if (disposable != null) {
                disposable.dispose();
            }
        } else {
            BuyInFragment fragment = (BuyInFragment) fragments.get(0);
            BuyInFragment fragment1 = (BuyInFragment) fragments.get(1);
            fragment.tvAmount.setText("1");
            fragment1.tvAmount.setText("1");
//            if (isLogin() & started) {
//                startInterval();
//            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (disposable != null) {
            disposable.dispose();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    public static TransactionHomeFragment newInstance() {
        TransactionHomeFragment fragment = new TransactionHomeFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void setAccountInfo(User data) {
//        userViewModel.setUser(getContext(), data);
        LiveDataBus.get().with(1,User.class).postValue(data);

        // 房 ---隐藏
//        setText(tvDynamicRights, data.getStockUserInfo().getUsableFund());
//        setText(tvFreezePromiseMoney, data.getStockUserInfo().getFrostDeposit());
//        if (data.getStockUserInfo().getProportion() != null) {
//
//        }
//        setText(tvHazardRate, data.getStockUserInfo().getProportion());
    }

    public void startInterval() {
        started = true;
        Observable.interval(10, TimeUnit.SECONDS)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        mPresenter.getAccountDetail(getAccount(), false);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void setSysInfo(SystemInfo data) {
        baoPer = data.getCloseOutRate();
    }

}
